﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertaNumbertoHexadecimal
{
    public class Solution
    {
        public string ToHex(int num)
        {
            uint n = (uint)num;
             if (num < 0) 
            {
                n = (uint)(~-num + 1);
            }

            var sb = new StringBuilder();
            while (n > 0)
            {
                var digit = n & 15;
                switch (digit)
                {
                    case 10:
                        sb.Insert(0, "a");
                        break;
                    case 11:
                        sb.Insert(0, "b");
                        break;
                    case 12:
                        sb.Insert(0, "c");
                        break;
                    case 13:
                        sb.Insert(0, "d");
                        break;
                    case 14:
                        sb.Insert(0, "e");
                        break;
                    case 15:
                        sb.Insert(0, "f");
                        break;
                    default:
                        sb.Insert(0, digit);
                        break;
                }
                n >>= 4;
            }

            return sb.Length > 0 ? sb.ToString() : "0";
        }
    }
}
